//
//  GRReceiveAddressViewController.m
//  YC_GRice
//
//  Created by luojinrong11 on 2018/7/9.
//  Copyright © 2018年 YeCai. All rights reserved.
//

#import "GRReceiveAddressViewController.h"
#import "GRChooseLocationViewController.h"
#import "GRMyAddressModel.h"
#import "GRBlankSpace.h"
@interface GRReceiveAddressViewController ()<UITextViewDelegate,UITextFieldDelegate>
@property(nonatomic,strong)GRMyAddressModel *proviceModel;
@property(nonatomic,strong)GRMyAddressModel *cityModel;
@property(nonatomic,strong)GRMyAddressModel *districtModel;

@property (weak, nonatomic) IBOutlet UITextField *addressTF;
@property (weak, nonatomic) IBOutlet UITextField *nameTF;
@property (weak, nonatomic) IBOutlet UITextField *phoneNumTF;

@property (weak, nonatomic) IBOutlet UILabel *ploceHolderTextView;

@property (weak, nonatomic) IBOutlet UITextView *detailAddressTextView;
@property (weak, nonatomic) IBOutlet UIButton *defaultAddBt;
@property(nonatomic,strong)GRMyAddressEditorModel * editorModel;

@property (weak, nonatomic) IBOutlet UILabel *titleLabel;

@property (weak, nonatomic) IBOutlet UIButton *submitBt;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topOffset;


@end

@implementation GRReceiveAddressViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.topOffset.constant = IS_IPHONEX ?88 :64;
    _districtModel = [[GRMyAddressModel alloc]init];
    _cityModel = [[GRMyAddressModel alloc]init];
    _proviceModel = [[GRMyAddressModel alloc]init];
    if (_editor) {
        [self getAddresInfoWithAddressId:self.addressId];
    }
    self.detailAddressTextView.delegate = self;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChange:) name:UITextFieldTextDidChangeNotification object:nil];
    
}

-(void)changeSubmitBtBK{
    if (self.phoneNumTF.text.length >0 && self.nameTF.text.length >0 && self.addressTF.text.length>0 && self.detailAddressTextView.text.length >0) {
        self.submitBt.backgroundColor = HEXCOLOR(0xFBD62D);
        self.submitBt.userInteractionEnabled = YES;
        [self.submitBt setTitleColor:HEXCOLOR(0x222222) forState:UIControlStateNormal];
    }else{
        self.submitBt.backgroundColor = HEXCOLOR(0xeeeeee);
        self.submitBt.userInteractionEnabled = NO;
        [self.submitBt setTitleColor:HEXCOLOR(0x999999) forState:UIControlStateNormal];
    }
}

-(void)textChange:(NSNotification *)notification{
    [self changeSubmitBtBK];
    
}

-(void)getAddresInfoWithAddressId:(NSInteger)addressId{
    [RequestManager getAddressInfo:addressId ComplectionHander:^(BOOL success, id responseObject, GRError *error) {
        if (success) {
            if (Dictionary(responseObject)) {
                self.editorModel = [GRMyAddressEditorModel yy_modelWithDictionary:responseObject];
                [self updateUI];
            }
        }
    }];
}

- (void)textViewDidChange:(UITextView *)textView{
    if (textView.text.length == 0) {
        self.ploceHolderTextView.hidden = NO;
    }else{
        self.ploceHolderTextView.hidden = YES;
    }
    [self changeSubmitBtBK];
}

-(void)updateUI{
    self.nameTF.text = self.editorModel.consigneeName;
    self.phoneNumTF.text = self.editorModel.consigneeMobile;
    self.detailAddressTextView.text = self.editorModel.addressStr;
    self.ploceHolderTextView.hidden = self.editorModel.addressStr.length == 0?NO:YES;
    self.addressTF.text = [NSString stringWithFormat:@"%@%@%@",self.editorModel.province,self.editorModel.city,self.editorModel.area];
    self.defaultAddBt.selected = self.editorModel.isDefault;
    self.districtModel.name = self.editorModel.area;
    self.districtModel.id = self.editorModel.areaId;
    self.cityModel.name = self.editorModel.city;
    self.cityModel.id = self.editorModel.cityId;
    self.proviceModel.name = self.editorModel.province;
    self.proviceModel.id = self.editorModel.provinceId;
    [self changeSubmitBtBK];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)locationAction:(UIButton *)sender {
    GRChooseLocationViewController * locationVC = [[GRChooseLocationViewController alloc]init];
    WEAKSELF
    locationVC.chooseFinish = ^(GRMyAddressModel *proviceModel, GRMyAddressModel *cityModel, GRMyAddressModel *districtModel) {
        weakSelf.proviceModel = proviceModel;
        weakSelf.cityModel = cityModel;
        weakSelf.districtModel = districtModel;
        weakSelf.addressTF.text = [NSString stringWithFormat:@"%@%@%@",proviceModel.name,cityModel.name,districtModel.name];
        [self changeSubmitBtBK];
    };
    [locationVC showInContoller:self];
}

- (IBAction)submitAction:(UIButton *)sender {
    sender.userInteractionEnabled = NO;
    NSDictionary * dic = @{
                           @"addr":self.detailAddressTextView.text,
                           @"area":self.districtModel.name?self.districtModel.name:@"",
                           @"areaId":@(self.districtModel.id),
                           @"city":self.cityModel.name,
                           @"cityId":@(self.cityModel.id),
                           @"consigneeMobile":self.phoneNumTF.text,
                           @"consigneeName":self.nameTF.text,
                           @"isDefault":@(self.defaultAddBt.selected?1:0),
                           @"province":self.proviceModel.name,
                           @"provinceId":@(self.proviceModel.id)
                           };
    
    if (_editor) {
        NSMutableDictionary * mutDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:@(self.addressId),@"id" ,nil];
        [mutDic addEntriesFromDictionary:dic];
        [self updateAddressAction: mutDic ];
    }else{
        [self adddressAction:dic];
    }
}

-(void)updateAddressAction:(NSDictionary *)dic{
        WEAKSELF
    [RequestManager updateAddress:dic ComplectionHander:^(BOOL success, id responseObject, GRError *error) {
        weakSelf.submitBt.userInteractionEnabled = YES;
        if (success) {
            [GRHUD showMessage:@"修改地址成功"];
            if (weakSelf.createAddrFinish != nil) {
                weakSelf.createAddrFinish();
            }
            [self.navigationController popViewControllerAnimated:YES];
        }else{
             [GRHUD showMessage:error.errorMsg];
        }
    }];
}

-(void)adddressAction:(NSDictionary *)dic{
 
    WEAKSELF
    [RequestManager createAddress:dic ComplectionHander:^(BOOL success, id responseObject, GRError *error) {
        weakSelf.submitBt.userInteractionEnabled = YES;
        if (success) {
            NSLog(@"创建地址成功");
            [GRHUD showMessage:@"创建地址成功"];
        
            if (weakSelf.createAddrFinish != nil) {
                weakSelf.createAddrFinish();
            }
        [self.navigationController popViewControllerAnimated:YES];
        }else{
            [GRHUD showMessage:error.errorMsg];
        }
    }];
    
}


- (IBAction)defaultAddressAction:(UIButton *)sender {
    sender.selected = !sender.selected;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
